package com.tal.bcc.pad.cloud.utils;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;


import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * author : zhangyang
 * date : 2022/10/24 17:22
 * description :
 */
public class JsonUtil {
    private static Gson sGson = new Gson();

    /**
     * 将对象转成json
     *
     * @param obj
     * @return
     */
    public static String objectToJson(Object obj) {
        if (obj != null) {
            try {
                return sGson.toJson(obj);
//                return JSON.toJSONString(obj);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "";
    }

    /**
     * 把json数据解析成对象
     *
     * @param json
     * @param type
     * @return
     */
    public static <T> T jsonToObject(String json, Type type) {
        if (!TextUtils.isEmpty(json)) {
            try {
                return sGson.fromJson(json, type);
//                return JSON.parseObject(json, clazz);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 将json数据转成bean
     *
     * @param jsonString
     * @param cls
     * @return T
     */
    public static <T> T getEntityFromJson(String jsonString, Class<T> cls) {
        T t = null;
        try {
            Gson gson = new Gson();
            t = gson.fromJson(jsonString, cls);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }



    /**
     * 解析类型为List<Map<String,Object>>的对象
     *
     * @param jsonString
     * @return List<Map<String,Object>>
     */
    public static List<Map<String, Object>> getListKeyMaps(String jsonString) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        try {
            Gson gson = new Gson();
            list = gson.fromJson(jsonString,
                    new com.google.gson.reflect.TypeToken<List<Map<String, Object>>>() {
                    }.getType());
        } catch (Exception e) {
        }
        return list;
    }

    /**
     * 将json串转换成list对象
     *
     * @param json
     * @param cls
     * @return ArrayList<T>
     * @author --ZhangJiQiang
     * @date 2016-3-7
     */
    public static <T> ArrayList<T> fromJsonList(String json, Class<T> cls) {
        Gson gson = new Gson();
        ArrayList<T> mList = new ArrayList<T>();
        JsonArray array = new JsonParser().parse(json).getAsJsonArray();

        for (final JsonElement elem : array) {
            mList.add(gson.fromJson(elem, cls));
        }

        return mList;
    }

    /**
     * json 转 map
     * @param jsonString
     * @return
     */
    public static Map<String, String> jsonToMap(String jsonString) {
        Map<String, String> map = null;
        try {
            Gson gson = new Gson();
            if (gson != null) {
                map = (Map)gson.fromJson(jsonString, (new TypeToken<Map<String, String>>() {
                }).getType());
            }
        } catch (Exception e) {

        }
        return map;
    }

    /**
     * 对象转为json string
     *
     * @param obj
     * @return
     */
    public static String toJson(Object obj) {
        String json = "";
        try {
            Gson gson = new Gson();
            json = gson.toJson(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
    public static String GsonString(Object object) {
        String gsonString = null;
        if (sGson != null) {
            gsonString = sGson.toJson(object);
        }

        return gsonString;
    }
    public static <T> T GsonToBean(String gsonString, Class<T> cls) {
        T t = null;
        if (sGson != null) {
            t = sGson.fromJson(gsonString, cls);
        }

        return t;
    }

    public static <T> Map<String, T> GsonToMaps(String gsonString) {
        Map<String, T> map = null;
        if (sGson != null) {
            map = (Map)sGson.fromJson(gsonString, (new TypeToken<Map<String, T>>() {
            }).getType());
        }

        return map;
    }

    public static <T> List<Map<String, T>> GsonToListMaps(String gsonString) {
        List<Map<String, T>> list = null;
        if (sGson != null) {
            list = (List)sGson.fromJson(gsonString, (new TypeToken<List<Map<String, T>>>() {
            }).getType());
        }

        return list;
    }

    public static <T> List<T> GsonToList(String gsonString, Class<T[]> cls) {
        if (sGson != null) {
            T[] arr = (new Gson()).fromJson(gsonString, cls);
            List<T> list = new ArrayList(Arrays.asList(arr));
            return list;
        } else {
            return null;
        }
    }
}
